import { useState } from 'react'
import { getLogInfos } from '@/api/system'
const useViewLogs = (
  bizKey:
    | 'id'
    | 'code'
    | 'supplierId'
    | 'planingId'
    | 'spuDevId'
    | 'spuCode'
    | 'purchaseOrderCode'
    | 'qualityOrderCode'
    | 'reserveInvoiceCode'
    | 'intentOrderCode'
    | 'quotationOrderId',
  type: string,
) => {
  const [isLogShow, setisLogShow] = useState(false)
  const [logLoading, setlogLoading] = useState(false)
  const [logInfos, setLogInfos] = useState<any[]>([])
  const viewLogs = async (info: any) => {
    setisLogShow(true)
    setlogLoading(true)
    const res = await getLogInfos({ bizNo: info[bizKey], type }).finally(() => setlogLoading(false))
    if (res.code === 200) {
      const loginfos = res?.data?.map((item: any) => {
        return {
          label: item.updateTime,
          children: (
            <div>
              <div style={{ display: 'flex' }}>
                <span style={{ flexShrink: 0, width: 70 }}>操作人员：</span>
                <span>{item.operatorName || '-'}</span>
              </div>
              <div style={{ display: 'flex' }}>
                <div style={{ flexShrink: 0, width: 70 }}>操作内容：</div>
                <div dangerouslySetInnerHTML={{ __html: '' + (item.logAction || '-') }}></div>
              </div>
              <div style={{ display: 'flex' }}>
                <span style={{ flexShrink: 0, width: 70 }}>操作时间：</span>
                <span>{item.createTime || '-'}</span>
              </div>
            </div>
          ),
        }
      })
      setLogInfos(loginfos)
    }
  }

  return { isLogShow, setisLogShow, logLoading, logInfos, setLogInfos, viewLogs }
}

export default useViewLogs
